package com.liyingjie.spzx.manager.util;

import com.liyingjie.spzx.model.entity.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2023-11-10-9:52
 */
public class MenuHelper {


    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
    //创建一个返回结果
        List<SysMenu> sysMenus = new ArrayList<>();
        //遍历集合，封装集合
        for (SysMenu sysMenu : sysMenuList) {
            if(sysMenu.getParentId().longValue()==0){
                sysMenus.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return sysMenus;
    }

    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> sysMenuList) {

        //初始化Children
        sysMenu.setChildren(new ArrayList<SysMenu>());
        //遍历集合
        for (SysMenu menu : sysMenuList) {
            if(sysMenu.getId().longValue()==menu.getParentId().longValue()){
                List<SysMenu> children = sysMenu.getChildren();
                children.add(findChildren(menu,sysMenuList));
            }

        }
        return sysMenu;

    }


}
